package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.SchoolNeeds;
import com.xmy.cultivate.entity.SchoolSort;
import com.xmy.cultivate.entity.Transcript;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.ISchoolNeedsService;
import com.xmy.cultivate.service.ISchoolSortService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 排序设置 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-12-11
 */
@RestController
@RequestMapping("/api/schoolSort")
public class SchoolSortController extends BaseController {

    @Autowired
    ISchoolSortService iSchoolSortService;

    @Autowired
    ISchoolNeedsService iSchoolNeedsService;

    @Autowired
    IQuarterService iQuarterService;

    @GetMapping()
    public Result getSchoolSort(@RequestParam(value = "pageNum", defaultValue = "-1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<SchoolSort> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper<SchoolSort> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*,( SELECT NAME FROM organization WHERE id = adms_school_sort.school_id LIMIT 1 ) AS school_name ");
        queryWrapper.orderByAsc("sort");
        iSchoolSortService.page(iPage, queryWrapper);

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    @PostMapping("/saveSchoolSort")
    public Result saveSchoolSort(@RequestBody SchoolSort schoolSort) {

        QueryWrapper<SchoolSort>  queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("school_id", schoolSort.getSchoolId());
        long count = iSchoolSortService.count(queryWrapper);

        if (count > 0){
            return new Result(Code.EXP_ERROR, null, "该校区已存在,请勿重复添加");
        }
        //查询排序是否存在
        QueryWrapper<SchoolSort> queryWrapperSort = new QueryWrapper<>();
        queryWrapperSort.eq("sort", schoolSort.getSort());
        if (iSchoolSortService.count(queryWrapperSort) > 0){
            return new Result(Code.EXP_ERROR, null, "该排序已被其他校区设置,请选择其他排序");
        }
        boolean save = iSchoolSortService.save(schoolSort);
        if (!save){
            return new Result(Code.ERROR, null, "添加失败");
        }
        return new Result(Code.OK, "", "添加成功");
    }


    @GetMapping("/updateSchoolSort")
    public Result updateSchoolSort(@RequestBody SchoolSort schoolSort) {
        SchoolSort schoolSort1 = iSchoolSortService.getById(schoolSort.getSchoolId());
        if (schoolSort1 == null){
            return new Result(Code.EXP_ERROR, null, "该条数据不存在");
        }
        boolean update = iSchoolSortService.updateById(schoolSort);
        if (!update){
            return new Result(Code.ERROR, null, "修改失败");
        }
        return new Result(Code.OK, "", "修改成功");
    }

    @DeleteMapping("")
    public Result deleteSchoolSort(@RequestParam("id") String id) {
        SchoolSort schoolSort = iSchoolSortService.getById(id);
        if (schoolSort == null){
            return new Result(Code.EXP_ERROR, null, "该数据不存在");
        }
        // 判断是否已经有招聘需求
        QueryWrapper<SchoolNeeds> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("school_id", schoolSort.getSchoolId());
        Long count = iSchoolNeedsService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.EXP_ERROR, null, "该校区已有招聘需求，无法删除");
        }
        iSchoolSortService.removeById(id);
        return new Result(Code.DELETE_OK, null, Code.DELETE_OK_MSG);
    }

}
